CXX=g++
CXXFLAGS=-Wall -Wextra -Winit-self -Wstrict-aliasing=2 -Wfloat-equal -Wno-comment -Wno-missing-field-initializers -Wno-unused -fPIC -DPTRACING=1 -D_REENTRANT -felide-constructors -Wreorder -DUNIT_TEST -I../common/include -Iinclude -I../embedding/include -I../protocols/include -I../factory/include
LDFLAGS=-L../factory/build-$(ARCH) -lfactory -L../protocols/build-$(ARCH) -lprotocols -L../embedding/build-$(ARCH) -lembedding -L../common/build-$(ARCH) -lcommon -L/usr/local/libnet-1.1.4/lib -lfftw3 -lgsl -lgslcblas -lnet -lnetfilter_queue -lnfnetlink -lpthread -lcryptopp
ifeq ($(DEBUG_BUILD),yes)
CXXFLAGS += -ggdb -O0 -DDEBUG -DVERBOSE_DEBUG=1
endif # DEBUG_BUILD
CXXFLAGS += $(CXXFLAGS_ARCH)
LDFLAGS += $(LDFLAGS_ARCH)
ifndef $(ARCH)
ARCH = default
endif

RM=rm
RMFLAGS=-rf
MKDIR=mkdir
AR=ar
ARFLAGS=rcs

SOUND_SUBDIR=sound
WAVE_SUBDIR=wave
ROUTING_SUBDIR=routing
CODEC_SUBDIR=codec

IO_BIN=$(BUILD_DIR)/stegit-2
IO_OBJ=$(BUILD_DIR)/main.o \
				$(BUILD_DIR)/samples_preprocessor.o \
				$(BUILD_DIR)/combination_io.o \
				$(BUILD_DIR)/original_modified_io.o \
				$(BUILD_DIR)/deinterleave_io.o \
				$(BUILD_DIR)/socket_io.o \
				$(BUILD_DIR)/byte_ring_buffer.o \
				$(BUILD_DIR)/$(WAVE_SUBDIR)/wave_io.o \
				$(BUILD_DIR)/$(WAVE_SUBDIR)/riff_wave.o \
				$(BUILD_DIR)/$(WAVE_SUBDIR)/riff_wave_writer.o \
				$(BUILD_DIR)/$(ROUTING_SUBDIR)/routing_io.o \
				$(BUILD_DIR)/$(ROUTING_SUBDIR)/rtp_packet.o \
				$(BUILD_DIR)/$(ROUTING_SUBDIR)/ip_packet.o \
				$(BUILD_DIR)/$(CODEC_SUBDIR)/pcm_codec.o \
				$(BUILD_DIR)/$(CODEC_SUBDIR)/alaw_codec.o \
				$(BUILD_DIR)/$(CODEC_SUBDIR)/ulaw_codec.o \

ifeq ($(ALSA_SUPPORT),yes)
LDFLAGS += -lasound
IO_OBJ += $(BUILD_DIR)/$(SOUND_SUBDIR)/alsa_io.o
CXXFLAGS += -D_STEGIT_ALSA_SUPPORT
endif

ifeq ($(PULSE_SUPPORT),yes)
LDFLAGS += -lpulse-simple
IO_OBJ += $(BUILD_DIR)/$(SOUND_SUBDIR)/pulse_audio_io.o
CXXFLAGS += -D_STEGIT_PULSE_SUPPORT
endif

SOURCE_DIR=src
BUILD_DIR=build-$(ARCH)

default: debug

release: all

all: $(IO_BIN)

debug:
	$(MAKE) DEBUG_BUILD=yes all
	
$(IO_BIN): init $(IO_OBJ)
	$(CXX) -o $(IO_BIN) $(IO_OBJ) $(LDFLAGS)
	
$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.cxx
	$(CXX) -c $(CXXFLAGS) -o $@ $<
	
init:
	@if [ ! -d $(BUILD_DIR) ] ; then $(MKDIR) -p $(BUILD_DIR) ; fi
	@if [ ! -d $(BUILD_DIR)/$(SOUND_SUBDIR) ] ; then $(MKDIR) -p $(BUILD_DIR)/$(SOUND_SUBDIR) ; fi
	@if [ ! -d $(BUILD_DIR)/$(WAVE_SUBDIR) ] ; then $(MKDIR) -p $(BUILD_DIR)/$(WAVE_SUBDIR) ; fi
	@if [ ! -d $(BUILD_DIR)/$(ROUTING_SUBDIR) ] ; then $(MKDIR) -p $(BUILD_DIR)/$(ROUTING_SUBDIR) ; fi
	@if [ ! -d $(BUILD_DIR)/$(CODEC_SUBDIR) ] ; then $(MKDIR) -p $(BUILD_DIR)/$(CODEC_SUBDIR) ; fi

.PHONY: clean

clean:
	$(RM) $(RMFLAGS) $(BUILD_DIR)

